package com.lw.yygh.order.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lw.yygh.enums.OrderStatusEnum;
import com.lw.yygh.enums.PaymentStatusEnum;
import com.lw.yygh.enums.PaymentTypeEnum;
import com.lw.yygh.hosp.feign.HospFeignClient;
import com.lw.yygh.model.order.OrderInfo;
import com.lw.yygh.model.order.PaymentInfo;
import com.lw.yygh.model.order.SignInfoVo;
import com.lw.yygh.order.mapper.PaymentMapper;
import com.lw.yygh.order.service.OrderService;
import com.lw.yygh.order.service.PaymentService;
import com.lw.yygh.utils.HttpRequestHelper;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, PaymentInfo> implements PaymentService {
    @Autowired
    private OrderService orderService;
    @Autowired
    private HospFeignClient hospFeignClient;
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer satus) {
        //根据订单id和和支付类型，查询支付记录表里是否含有相同记录
        QueryWrapper<PaymentInfo> wrapper = new QueryWrapper<>();
        wrapper.eq( "order_id",orderInfo.getId() );
        wrapper.eq( "payment_type",satus );
        Integer count = baseMapper.selectCount( wrapper );
        if (count>0){
            return;
        }
        //添加记录
        // 保存交易记录
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(satus);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        paymentInfo.setPaymentStatus( PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")+"|"+orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        baseMapper.insert(paymentInfo);
    }

    @Override
    public void paySuccess(String out_trade_no, Integer status, Map<String, String> resultMap) {
        //更新订单支付状态
        OrderInfo orderInfo = orderService.getOrder( out_trade_no );
        orderInfo.setOrderStatus( OrderStatusEnum.PAID.getStatus() );
        orderService.updateById( orderInfo );
        //更新支付记录信息
        QueryWrapper<PaymentInfo> wrapper=new QueryWrapper<>();
        wrapper.eq( "out_trade_no",out_trade_no );
        wrapper.eq( "payment_type", PaymentTypeEnum.WEIXIN.getStatus() );
        PaymentInfo paymentInfo = baseMapper.selectOne( wrapper );
        paymentInfo.setPaymentStatus( PaymentStatusEnum.PAID.getStatus() );
        paymentInfo.setCallbackTime( new Date() );
        paymentInfo.setTradeNo( resultMap.get( "transaction_id" ) );
        paymentInfo.setCallbackContent( resultMap.toString() );
        baseMapper.updateById( paymentInfo );
        //调用医院接口，更新订单支付状态
        SignInfoVo signInfoVo = hospFeignClient.getSignInfoVo( orderInfo.getHoscode() );
        Map<String,Object> reqMap=new HashMap<>();
        reqMap.put("hoscode",orderInfo.getHoscode());
        reqMap.put("hosRecordId",orderInfo.getHosRecordId());
        reqMap.put("timestamp", HttpRequestHelper.getTimestamp());
        String sign = HttpRequestHelper.getSign(reqMap, signInfoVo.getSignKey());
        reqMap.put("sign", sign);
        JSONObject result = HttpRequestHelper.sendRequest( reqMap, "http://localhost:9998/order/updatePayStatus" );
    }

    //根据订单编号和支付类型查找支付记录
    @Override
    public PaymentInfo getPaymentInfo(String orderId, Integer paymentType) {
        QueryWrapper<PaymentInfo> wrapper=new QueryWrapper<>();
        wrapper.eq( "out_trade_no",orderId );
        wrapper.eq( "payment_type",paymentType );
        PaymentInfo paymentInfo = baseMapper.selectOne( wrapper );
        return paymentInfo;
    }
}
